//
//  GYDTabBarController.swift
//  Weibo
//
//  Created by 贵永冬 on 16/7/14.
//  Copyright © 2016年 贵永冬. All rights reserved.
//

import UIKit

class GYDTabBarController: UITabBarController {
    
    
    // MARK: - 懒加载自定义tabbar
    private lazy var coustomTabBar: GYDTabBar = {
        () -> GYDTabBar in
        return GYDTabBar()
    }()
    
    private lazy var tabBarViewModel : GYDTabBarViewModel = {
        () -> GYDTabBarViewModel in
        return GYDTabBarViewModel(tabBarVC:self)
    }()
    
    
    override func viewDidLoad() {
        
        //自定义tabbar
        self.addCustomTabBar()
        
        //添加所有的子控制器
        self.addAllChildVCs()
        
    }
    
    
    struct Static {
        static var onceToken : dispatch_once_t = 0
    }
    
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        dispatch_once(&Static.onceToken) {
            if GYDUserModel.sharedInstance.isLogin {
                //显示欢迎界面
                GYDWelcomeViewController.showWelcomeVC()
            }
        }
    }
    
    /**
     自定义tabbar
     */
    private func addCustomTabBar() {
        self.setValue(self.coustomTabBar, forKey: "tabBar")
        coustomTabBar.addButtonBlock = { [unowned self] () -> Void in
            self.tabBarViewModel.showPublishView()
        }
    }
    
    /**
     添加所有的子控制器
     */
    private func addAllChildVCs() {
        self.addChildViewContrller("GYDHomeViewController", title: "首页", normalImageName: "tabbar_home", selectedImageName: "tabbar_home_selected")
        self.addChildViewContrller("GYDMessageViewController", title: "消息", normalImageName: "tabbar_message_center", selectedImageName: "tabbar_message_center_selected")
        self.addChildViewContrller("GYDFoundViewController", title: "发现", normalImageName: "tabbar_discover", selectedImageName: "tabbar_discover_selected")
        self.addChildViewContrller("GYDProfileViewController", title: "我", normalImageName: "tabbar_profile", selectedImageName: "tabbar_profile_selected")
    }
    /**
     添加子控制器
     - parameter childViewContrllerName: 子控制器的名称
     - parameter title:                  title
     - parameter normalImageName:        未选中状态图标名
     - parameter selectedImageName:      选中状态图标名
     */
    private func addChildViewContrller(childViewContrllerName: String, title: String, normalImageName: String, selectedImageName: String) {
        //动态获取命名空间
        //由于字典中 数组中 只能存储对象 所以取值的时候 是一个 AnyObject类型 返回值可能有值页可能没值
        //守护   解决可选绑定的条件嵌套
        guard let workSpaceName = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as? String else {
            //获取命名空间失败
            return
        }
        let cls: AnyClass? = NSClassFromString(workSpaceName + "." + childViewContrllerName)
        
        guard let typeCls = cls as? UIViewController.Type else {
            //转UIViewController 失败
            return
        }
        //创建对象
        let childVC = typeCls.init()
        childVC.title = title
        childVC.tabBarItem.image = UIImage.imageWithOriginal(normalImageName)
        childVC.tabBarItem.selectedImage = UIImage.imageWithOriginal(selectedImageName)
        
        
        let shadow = NSShadow()
        shadow.shadowOffset = CGSize.zero
        
        let normalAttributes = [NSForegroundColorAttributeName:RGB(101, g: 101, b: 101),NSFontAttributeName:UIFont.systemFontOfSize(11),NSShadowAttributeName:shadow]
        let selectedAttributes = [NSForegroundColorAttributeName:RGB(254, g: 118, b: 1),NSFontAttributeName:UIFont.systemFontOfSize(11),NSShadowAttributeName:shadow]
        
        childVC.tabBarItem.setTitleTextAttributes(normalAttributes, forState: UIControlState.Normal)
        childVC.tabBarItem.setTitleTextAttributes(selectedAttributes, forState: UIControlState.Selected)
        
        let nav = GYDNavigationController(rootViewController: childVC)
        self.addChildViewController(nav)
        
    }
}


